/*
 * ControlSquare.fx
 *
 * Created on 26.10.2009, 23:32:17
 */

package justforfun.bombsquare;

import javafx.scene.Node;
import javafx.scene.CustomNode;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.KeyCode;
import java.lang.Math.abs;
import justforfun.bombsquare.Square;
import justforfun.bombsquare.Constants.*;
import justforfun.bombsquare.myNode;
import javafx.scene.input.MouseEvent;
import java.lang.Math;

/**
 * @author civil
 */

public class ControlSquareBind extends CustomNode {

    public var height: Float = 0;
    public var width: Float = 0;
    public var x: Float = 0;
    public var y: Float = 0;
    public var action: function () : Void = null;
    public var square: Square;
    public var field: myNode[];

    var stepVal = bind square.stepVal;

    var up = function () {
        var nextY = (square.y - stepVal);
        def pPretend = function (obj: myNode) {
            var deltaX: Float = Math.abs(obj.x - square.x);
            (deltaX >= 0 and deltaX < 1) and obj.y == nextY.intValue() and not obj.vincible
        }

        var countPretend = field[obj | pPretend(obj)].size();

        if (nextY * my >= y and countPretend == 0) {
            square.up();
        }
    }

    var down = function () {
        var nextY = (square.y + stepVal);
        def pPretend = function (obj: myNode) {
            var deltaX: Float = Math.abs(obj.x - square.x);
            (deltaX >= 0 and deltaX < 1) and obj.y == square.y.intValue() + 1 and not obj.vincible
        }

        var countPretend = field[obj | pPretend(obj)].size();

        if (nextY * my <= y + height and countPretend == 0) {
            square.down();
        }
    }

    var right = function () {
        var nextX = (square.x + stepVal);
        def pPretend = function (obj: myNode) {
            var deltaY: Float = Math.abs(obj.y - square.y);
            (deltaY >= 0 and deltaY < 1) and obj.x == square.x.intValue() + 1 and not obj.vincible
        }

        var countPretend = field[obj | pPretend(obj)].size();

        if (nextX * mx <= x + width and countPretend == 0) {
            square.right();
        }
    }

    var left = function () {
        var nextX = (square.x - stepVal);
        def pPretend = function (obj: myNode) {
            var deltaY: Float = Math.abs(obj.y - square.y);
            (deltaY >= 0 and deltaY < 1) and obj.x == nextX.intValue() and not obj.vincible
        }

        var countPretend = field[obj | pPretend(obj)].size();

        if (nextX * mx >= x and countPretend == 0) {
            square.left();
        }
    }

    var control = function (ke: KeyEvent) {
        var key = ke.code;

        if (key == KeyCode.VK_SPACE and action != null) {
            action();
        } else if (key == KeyCode.VK_UP) {
            up();
        } else if (key == KeyCode.VK_DOWN) {
            down();
        } else if (key == KeyCode.VK_RIGHT) {
            right();
        } else if (key == KeyCode.VK_LEFT) {
            left();
        }
    }

    override public function create (): Node {
        var rec: Rectangle = Rectangle {
            onKeyPressed: control
            focusTraversable: true
            fill: null
            

        }
    }
}
